/*
*    Phantom v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
// File.cpp: implementation of the File class.
//
//////////////////////////////////////////////////////////////////////

#include "ssi_file.h"
#include "ssi_internals.h"
#include "ssi_file.h"
#include <direct.h>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

File::File()
{
  fh = SSI_NULL;
}

File::~File()
{
  if(fh)
  {
    fclose(fh);
  }
}

File::File(const String& FileName)
{
  Status = Open(FileName, "r");
}

CStatus File::Open(const String& FileName, String Mode)
{
  if(FileName.GetLength() <= 0){
    MAKEERR0(ERR03401);
  }

  if(Mode.GetLength() <= 0){
    MAKEERR0(ERR03401);
  }


  fh = fopen(FileName.Str(), Mode.Str());
  if(!fh)
  {
		//MAKEERR1(ERR02100, FileName.Str());
    //return STAT_ERR;
    MAKEERR0(ERR03401);
  }
  Status = STAT_OK;
  Name = FileName;
  return Status;
}

SSI_INT64 File::ReadLine(String& theLine)
{
	if(!fh) return -1;
	// 1 SSI_DISPLAY_FILE_NEW
  char* buff = new char[MAX_SSI_CHARS];
  char* s;

  s = fgets(buff, MAX_SSI_CHARS, fh);

	if(!s){
		delete buff;
		return -1;
	}

  //theLine = buff;
	// Need to remove both newlines and carriage returns
	// This should fix a bug...
  // This will remove both...
  String t = buff;
  int iPos = t.Parse("\n\r", theLine);
  if(iPos < 0){
    theLine = t;
  }
	
  // This causes problems with 00 at end of string... dont know why
  //len = theLine.FindOneOf(String("\n\r"));
  //theLine = theLine.SubString(0, len-1);
	//if(len >= 0){
	//	theLine.SetAt(len, 0);
	//}


	//if(len >= 1){
	//	if(theLine[len-1] == '\n' || theLine[len-1] == '\r')
	//	{
	//		theLine.SetAt(len-1, 0);
	//	}
	//}

	//if(len >= 2){
	//	if(theLine[len-2] == '\n' || theLine[len-2] == '\r')
	//	{
	//		theLine.SetAt(len-2, 0);
	//	}
	//}

	delete buff;
	if(s)
  {	
    return theLine.GetLength();
  }
  return -1;
}

void File::SetHandle(FILE* inHandle)
{
	if(fh){
		fclose(fh);
	}
	fh = inHandle;
}

String File::GetPath(void)
{
  char aP[4096];
  char* path = _fullpath(aP, Name.Str(), 4095);
  if(path == NULL){
    return String(getcwd(aP, 4095));
  }
  String s = path;
  SSI_INT64 i = s.ReverseFind("\\");
  if(i>0){
    s = s.SubString(0,i-1);
    return s;
  }else{
    return String(getcwd(aP, 4095));
  }
}

CStatus File::WriteLine(String& Line)
{
  if(!fh){
    //return STAT_ERR;
    MAKEERR0(ERR03402);
  }
  fprintf(fh, "%s", Line.Str());
  return STAT_OK;
}




